package com.bit.demo.springbookdemo.service;

import com.bit.demo.springbookdemo.enums.BookStatusEnum;
import com.bit.demo.springbookdemo.mapper.BookMapper;
import com.bit.demo.springbookdemo.model.BookInfo;
import com.bit.demo.springbookdemo.model.PageRequest;
import com.bit.demo.springbookdemo.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public void addBook(BookInfo bookInfo){
        bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getBookByPage(PageRequest pageRequest){
        Integer count=bookMapper.count();
        List<BookInfo> bookLists=bookMapper.getListByPage(pageRequest);
        for(BookInfo bookInfo:bookLists){
            bookInfo.setStatusStr(BookStatusEnum.getByCode(bookInfo.getStatus()).getDesc());
        }
        return new ResponseResult<>(count,bookLists,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
